package sk.tuke.android4all.dialer;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnGenericMotionListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import sk.tuke.android4all.controls.CustomFragment;
import sk.tuke.android4all.listeners.SwipeListener;
import sk.tuke.android4all.services.Tts;

public class HistoryFragment extends CustomFragment {

	private static String TAG = "HistoryFragment";
	private HistoryAdapter mItems = null;
	private Activity mActivity = null;
	private String selectedTag = null;
	int touchCounter = 0;
	private final int TOUCH_INTENSITY = 4;
	private SwipeListener mActionListener = null;
	private Tts mSpeechEngine;
	
	public HistoryFragment(Activity aActivity_in){
		this.mActivity = aActivity_in;
		mItems = new HistoryAdapter(mActivity.getApplicationContext());
		mSpeechEngine = new Tts(mActivity);
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_history, container, false);	        
        return rootView;
    }
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       createUI();
    }
	
	private void createUI() {
		mActionListener =  new SwipeListener(mActivity) {
            @Override
            public void onDoubleTapAction(int count) { 
                Log.i("TAG", "DoubleTapped " + selectedTag);
                resetTouchCounter();
                if(selectedTag != null){
                	if(selectedTag == getResources().getString(R.string.fragmentcontacts_next)){
                		showNextItems();
                	}else{
                		mSpeechEngine.sayMessage(mItems.getContactInfo(selectedTag).getEvent().toString() + " " + mItems.getContactInfo(selectedTag).getNumber());
                		itemClickedAction(selectedTag);
                	}
                }
            }

			@Override
			public void onZoomInAction() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onZoomOutAction() {
				// TODO Auto-generated method stub
				
			}
		};
		getView().setOnTouchListener(mActionListener);
		
		resetItems();
		Log.d(TAG, "Scrool needend" + String.valueOf(mItems.scrollNeeded()));
		if(mItems.scrollNeeded()){
			setControl(R.id.fragmentcontacts_next, null, true);
		}else{
			((LinearLayout) getView()).removeView(getView().findViewById(R.id.fragmentcontacts_next));
		}
		if(mItems.isEmpty()){
			mSpeechEngine.sayMessage(getResources().getString(R.string.fragmenthistory_noitem));
		}
	}
	
	private void showNextItems() {
		mItems.ScrollDown();
		resetItems();
	}
	
	
	private void resetItems(){
		HistoryItem[] aItems = mItems.getCurrentSet();
		setControl(R.id.fragmenthistory_item1, aItems[0], false);
		setControl(R.id.fragmenthistory_item2, aItems[1], false);
		setControl(R.id.fragmenthistory_item3, aItems[2], false);
		setControl(R.id.fragmenthistory_item4, aItems[3], false);
		setControl(R.id.fragmenthistory_item5, aItems[4], false);
	}

	private void setControl(int controlId,final HistoryItem item, boolean isNavButton) {
		final TextView controlTv = (TextView) getView().findViewById(controlId);
		if(item != null){
			controlTv.setText(item.getName());	
		}else{
			if(!isNavButton){
				((LinearLayout) getView()).removeView(controlTv);
				return;
			}
		}
		controlTv.setOnGenericMotionListener(new OnGenericMotionListener(){

			@Override
			public boolean onGenericMotion(View arg0, MotionEvent arg1) {
				mActionListener.resetTouchCounter();
				String textInControl = controlTv.getText().toString();
				String actionString = item!=null?item.getId(): controlTv.getText().toString();
                if (selectedTag != actionString && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    mSpeechEngine.sayMessage(textInControl);
                    if(item!=null){
                    	selectedTag = item.getNumber();
                    }else{
                    	selectedTag = controlTv.getText().toString();
                    }
                }else{
                    touchCounter++;
                }
                return false;
			}});		
	}
	
	private void itemClickedAction(String selectedTag) {
		mSpeechEngine.sayMessage(getResources().getString(R.string.fragmentcontext_call));
		String callingNumber = "tel:" + selectedTag;
		Intent intent = new Intent(Intent.ACTION_CALL);
		intent.setData(Uri.parse(callingNumber));
		startActivity(intent);	
	}

	@Override
	public void returnAction() {
		mSpeechEngine.sayMessage(getResources().getString(R.string.fragmentdialer_intro));
		mActivity.getFragmentManager().beginTransaction().replace(R.id.container, new DialerFragment(mActivity), DialerFragment.class.getName()).commit();
	}

}
